<?php
##############################################
#
#   活动分类
# @ Time : 2017-2-28 - 下午11:39:25
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Model;

class ActivityCategoryModel extends IndexBaseModel{

	/*活动表  */
	protected $tableName = 'activity_category';
	protected $_field = array('id', 'activity_name', 'city', 'area', 'detail_address', 'activity_img', 'trial_status', 'sponsor', 'type', 'price', 'start_time', 'end_time', 'signup_number','category');
	
	/**
	  * 获取分类
	  * @date 2017年9月15日 - 上午11:37:02
	  * @author JiangYongWei
	 */
	public function getCategory(){
	    $fields = array('id','name','ico_img');
	    return $this->field($fields)->where("is_show = 1")->limit(6)->order('sort asc, id desc')->select();
	}
	
	/**
	 * 获取所有分类
	 * @date 2017年9月15日 - 上午11:37:02
	 * @author JiangYongWei
	 */
	public function getAllCategory(){
	    $fields = array('id','name','ico_img');
	    return $this->field($fields)->where("is_show = 1")->order('sort asc, id desc')->select();
	}
	
	/**
	 * 获取活动
	 */
	public function getActivity($city,$activity_name){
	    if($activity_name){
	        $map['activity_name'] = array('like','%'.$activity_name.'%');
	    }
	    $map = array('city'=>array('like','%'.$city.'%'),'is_show_index'=>1, 'trial_status'=>2);
	    $map['type'] =  array(array('eq',1),array('eq',2), 'or') ;
	    $count = $this -> where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 5);
	    $show = $Page->show();
	    $result = $this->field($this->_field)->where($map)->order('is_top desc, id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $this->getLastSql();
	    return array($result, $show);
	}
	
	/**
	 * 获取详情
	 */
	public function getActivityDetail($id) {
	    return $this->where("id = $id")->find();
	}
	
	/**
	 * 获取推荐活动
	 */
	public function getRecommend($city, $activity_id) {
	    return $this->field($this->_field)->where("id not in($activity_id) and city = '$city' and trial_status = 2")->limit(6)->order('add_time desc')->select();
	}
	
	/**
	 * 是否超过该活动最大人数限制
	 */
	public function isMaxPeople($activity_id) {
       return $this->where("id = $activity_id")->field('number, signup_number')->find();	     
	}
	
	/**
	 * 已报名人数加1
	 */
	public function setSignupNumber($activity_id) {
	    return $this->where("id = $activity_id")->setInc('signup_number');
	}

	/**
	  * 增加浏览量
	  * @param int $activity_id
	  * @date 2017年8月7日 - 上午11:30:48
	  * @author JiangYongWei
	 */
	public function browser($activity_id) {
	    return $this->where("id = $activity_id")->setInc('browse_number');
	}
	
	/**
	  * 最新活动
	  * @param int $user_id
	  * @date 2017年8月31日 - 下午5:25:06
	  * @author JiangYongWei
	 */
	public function newActivity($user_id){
	    $map = array('user_id'=>$user_id, 'trial_status'=>2, 'end_time'=>array('egt', time()));
	    $count = $this->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->field($this->_field)->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    return array($result, $show);
	}
	
	/**
	 * 全部活动
	 * @param int $user_id
	 * @date 2017年8月31日 - 下午5:25:06
	 * @author JiangYongWei
	 */
	public function allActivity($user_id){
	    $map = array('user_id'=>$user_id, 'type'=>2);// 'end_time'=>array('egt', time())
	    $count = $this->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->field($this->_field)->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    return array($result, $show);
	}
	
	/**
	 * 已发布活动
	 * @param int $user_id
	 * @date 2017年8月31日 - 下午5:25:06
	 * @author JiangYongWei
	 */
	public function release($user_id){
	    $map = array('user_id'=>$user_id, 'type'=>2, 'trial_status'=>2);// 'end_time'=>array('egt', time())
	    $count = $this->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->field($this->_field)->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $this->getLastSql();
	    return array($result, $show);
	}
	
	/**
	 * 待审核活动
	 * @param int $user_id
	 * @date 2017年8月31日 - 下午5:25:06
	 * @author JiangYongWei
	 */
	public function pendingAuditActivity($user_id){
	    $map = array('user_id'=>$user_id, 'type'=>2, 'trial_status'=>1);// 'end_time'=>array('egt', time())
	    $count = $this->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->field($this->_field)->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $this->getLastSql();
	    return array($result, $show);
	}
	
	/**
	 * 不通过活动
	 * @param int $user_id
	 * @date 2017年8月31日 - 下午5:25:06
	 * @author JiangYongWei
	 */
	public function notPassActivity($user_id){
	    $map = array('user_id'=>$user_id, 'type'=>2, 'trial_status'=>3);// 'end_time'=>array('egt', time())
	    $count = $this->where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, 1);
	    $show = $Page->show();
	    $result = $this->field($this->_field)->where($map)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    //echo $this->getLastSql();
	    return array($result, $show);
	}
	
	/**
	  * 修改活动
	  * @param int $activity_id
	  * @date 2017年9月8日 - 下午3:40:22
	  * @author JiangYongWei
	 */
	public function editActivity($activity_id) {
	    return $this->where("id = $activity_id")->find();
	}
	

}
